/*
 * MimeUtils.cpp
 *
 * Copyright 2017 Amazon.com, Inc. or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/// @file MimeUtils.cpp

#include "MimeUtils.h"

#include <AVSCommon/AVS/Attachment/InProcessAttachmentReader.h>

#include "Common.h"

namespace alexaClientSDK {
namespace acl {
namespace test {

using namespace avsCommon::avs::attachment;
using namespace avsCommon::sdkInterfaces;

/// The newline characters that MIME parsers expect.
static const std::string MIME_NEWLINE = "\r\n";
/// The double dashes which may occur before and after a boundary string.
static const std::string MIME_BOUNDARY_DASHES = "--";
/// The MIME text expected before a JSON part.
static const std::string MIME_JSON_PREFIX_STRING = "Content-Type: application/json; charset=UTF-8";
/// The MIME text expected before a binary data part.
static const std::string MIME_ATTACHMENT_PREFIX_STRING = "Content-Type: application/octet-stream";
/// The MIME prefix for a content id header.
static const std::string MIME_CONTENT_ID_PREFIX_STRING = "Content-ID: ";
/// Our default timeout when validating if a MIME part was received by another object.
static const std::chrono::seconds WAIT_FOR_DIRECTIVE_TIMEOUT_IN_SECONDS = std::chrono::seconds(10);

TestMimeJsonPart::TestMimeJsonPart(int dataSize, std::shared_ptr<TestableMessageObserver> messageObserver) :
        m_message{createRandomAlphabetString(dataSize)}, m_messageObserver{messageObserver} {
}

std::string TestMimeJsonPart::toMimeString(const std::string & boundaryString) {
    return MIME_BOUNDARY_DASHES + boundaryString + MIME_NEWLINE +
            MIME_JSON_PREFIX_STRING + MIME_NEWLINE + MIME_NEWLINE +
            m_message + MIME_NEWLINE;
}

bool TestMimeJsonPart::validateMimeParsing() {
    return m_messageObserver->waitForDirective(m_message, WAIT_FOR_DIRECTIVE_TIMEOUT_IN_SECONDS);
}

TestMimeAttachmentPart::TestMimeAttachmentPart(const std::string & contextId, const std::string contentId,
        int dataSize, std::shared_ptr<AttachmentManager> attachmentManager) :
                m_contextId{contextId}, m_contentId{contentId},
                m_attachmentData{createRandomAlphabetString(dataSize)}, m_attachmentManager{attachmentManager} {
}

std::string TestMimeAttachmentPart::toMimeString(const std::string & boundaryString) {
    return MIME_BOUNDARY_DASHES + boundaryString + MIME_NEWLINE +
            MIME_CONTENT_ID_PREFIX_STRING + m_contentId + MIME_NEWLINE +
            MIME_ATTACHMENT_PREFIX_STRING + MIME_NEWLINE + MIME_NEWLINE +
            m_attachmentData + MIME_NEWLINE;
}

bool TestMimeAttachmentPart::validateMimeParsing() {
    auto attachmentId = m_attachmentManager->generateAttachmentId(m_contextId, m_contentId);
    auto reader = m_attachmentManager->createReader(attachmentId, AttachmentReader::Policy::BLOCKING);

    std::vector<uint8_t> result(m_attachmentData.size());
    auto readStatus = InProcessAttachmentReader::ReadStatus::OK;
    auto numRead = reader->read(result.data(), result.size(), &readStatus);
    if (numRead != m_attachmentData.length()) {
        return false;
    }
    if (readStatus != InProcessAttachmentReader::ReadStatus::OK) {
        return false;
    }

    for (size_t i = 0; i < m_attachmentData.size(); ++i) {
        if (result[i] != m_attachmentData[i]) {
            return false;
        }
    }

    return true;
}

std::string constructTestMimeString(
        const std::vector<std::shared_ptr<TestMimePart>> & mimeParts, const std::string & boundaryString) {
    std::string mimeString;

    for (auto mimePart : mimeParts) {
        mimeString += mimePart->toMimeString(boundaryString);
    }

    // The final mime part needs the closing double dashes.
    mimeString += MIME_BOUNDARY_DASHES + boundaryString + MIME_BOUNDARY_DASHES + MIME_NEWLINE;

    return mimeString;
}

} // namespace test
} // namespace acl
} // namespace alexaClientSDK
